using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixFilterEngineDAO : Abstracts.AbstractGenericDAO<WixFilterEngine, System.Nullable<int>>,
        Interfaces.WIX.IWixFilterEngineDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "filter_engine_id";
        }

        /// <summary>
        /// Pobieranie opisu dla filtra na podstawie numeru filtra.
        /// </summary>
        /// <param name="filterId">Filtr</param>
        /// <returns>opis dla filtra</returns>
        public IQueryable<WixFilterEngine> FindByFilter(int filterId, int engineId)
        {
            if (filterId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilterEngine> description = this._cmsEntitiesDataContext.WixFilterEngines.Where(
                    fien => fien.filter_id.Equals(filterId) && fien.engine_id.Equals(engineId)
                        );
                if (!(description.Any()))
                {
                    return null;
                }   //if
                return description;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie opisu dla filtra na podstawie numeru silnika i modelu.
        /// </summary>
        /// <param name="filterId">Filtr</param>
        /// <returns>opis dla filtra</returns>
        public IQueryable<WixFilterEngine> FindByEngineAndModel(int engineId, int modelId)
        {
            if ((engineId.Equals(0)) || (modelId.Equals(0)))
            {
                return null;
            }
            try
            {
                IQueryable<WixFilterEngine> description = this._cmsEntitiesDataContext.WixFilterEngines.Where(
                    fien => fien.engine_id.Equals(engineId) && fien.model_id.Equals(modelId)
                        );
                if (!(description.Any()))
                {
                    return null;
                }   //if
                return description;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        //zwraca filtry bez daty i opisu
        public IQueryable<WixFilterEngine> FindByEngineAndModelOnlyNulls(int engineId, int modelId)
        {
            if ((engineId.Equals(0)) || (modelId.Equals(0)))
            {
                return null;
            }
            try
            {
                IQueryable<WixFilterEngine> description = this._cmsEntitiesDataContext.WixFilterEngines.Where(
                    fien => fien.engine_id.Equals(engineId) && fien.model_id.Equals(modelId) && (fien.year_to == null) && (fien.year_from == null)
                        && fien.filter_additional_info.Length < 2
                        );
                if (!(description.Any()))
                {
                    return null;
                }   //if
                return description;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixFilterEngine> FindByFilter(int filterId)
        {
            if (filterId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilterEngine> description = this._cmsEntitiesDataContext.WixFilterEngines.Where(
                    fien => fien.filter_id.Equals(filterId)
                        );
                if (!(description.Any()))
                {
                    return null;
                }   //if
                return description;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

    }
}
